<?php

App::uses('AppModel', 'Model');

/**

 * Property Model

 *

 * @property User $User

 * @property City $City

 * @property PropertyType $PropertyType

 */

class Property extends AppModel {



/**

 * Use table

 *

 * @var mixed False or table name

 */

	public $useTable = 'property';

// var $actsAs = array('MeioUpload.MeioUpload' => array('image'));





/* var $actsAs = array('MeioUpload.MeioUpload' => array(

        'image1' => array('dir' => 'files/images'),

        'image2' => array('dir' => 'files/images')

    ));  

*/



    public $actsAs = array(
            'MeioUpload.MeioUpload' => array(
              'image1' => array('dir' => 'files/property','thumbsizes' => array('thumb' => array('width' => 600, 'height' => 400))),
              'image2' => array('dir' => 'files/property','thumbsizes' => array('thumb' => array('width' => 600, 'height' => 400))),
              'image3' => array('dir' => 'files/property','thumbsizes' => array('thumb' => array('width' => 600, 'height' => 400))),
          )
        );


/**

 * Display field

 *

 * @var string

 */

	public $displayField = 'title';



/**

 * Validation rules

 *

 * @var array

 */

	public $validate = array(
		'title' => array(
			'notEmpty' => array(
				'rule' => array('notEmpty'),
				'message' => 'Title required',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'user_id' => array(
			'notEmpty' => array(
				'rule' => array('notEmpty'),
				'message' => 'Agen/User required',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'address' => array(
			'notEmpty' => array(
				'rule' => array('notEmpty'),
				'message' => 'Address required',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'location' => array(
			'notEmpty' => array(
				'rule' => array('notEmpty'),
				'message' => 'Location required',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'city_id' => array(
			'notEmpty' => array(
				'rule' => array('notEmpty'),
				'message' => 'City required',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'description' => array(
			'notEmpty' => array(
				'rule' => array('notEmpty'),
				'message' => 'Description required',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'published_on' => array(
			'datetime' => array(
				'rule' => array('date'),
				'message' => 'Date required',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'price' => array(
			'decimal' => array(
				'rule' => array('decimal'),
				'message' => 'Decimal value required',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'beds' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				'message' => 'Numeric value required',
				'allowEmpty' => true,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'baths' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				'message' => 'Numeric value required',
				'allowEmpty' => true,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'parking' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				'message' => 'Numeric value required',
				'allowEmpty' => true,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
        'contract_type' => array(
            'valid' => array(
                'rule' => array('inList', array('sale','rent')),
                'message' => 'Please select a valid Contract Type',
                'allowEmpty' => false
            )
        ),
        'property_type' => array(
			'notEmpty' => array(
				'rule' => array('notEmpty'),
				'message' => 'Please select a valid Property Type',
			),
		)		
        /*'property_type' => array(
            'valid' => array(
                'rule' => array('inList', array('apartment'=>'Apartment','villa'=>'Villa','townhouse'=>'Townhouse','penthouse'=>'Penthouse','compound'=>'Compound','duplex'=>'Duplex','full_floor'=>'Full Floor','half_floor'=>'Half Floor','whole_building'=>'Whole Building','land'=>'Land','bulk_sale_units'=>'Bulk Sale Units','bungalow'=>'Bungalow','hotel'=>'Hotel','hotel_apartment'=>'Hotel Apartment')),
                'message' => 'Please select a Valid Property Type',
                'allowEmpty' => true
            )
        )*/
	);



	//The Associations below have been created with all possible keys, those that are not needed can be removed



/**

 * belongsTo associations

 *

 * @var array

 */

	public $belongsTo = array(
		'User' => array(
			'className' => 'User',
			'foreignKey' => 'user_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'City' => array(
			'className' => 'City',
			'foreignKey' => 'city_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''

		),
		'PropertyType' => array(
			'className' => 'PropertyType',
			'foreignKey' => 'property_type_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);

    public function belongs_to() {
		$this->belongsTo = array(
		'User' => array(
			'className' => 'User',
			'foreignKey' => 'user_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'City' => array(
			'className' => 'City',
			'foreignKey' => 'city_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''

		)
	);
		
		}

    public function beforeSave($options = array()) {//pr($this->data); //exit;
        if (!empty($this->data[$this->alias]['title']) && empty($this->data[$this->alias]['slug'])) {
            if (!empty($this->data[$this->alias][$this->primaryKey])) {
                $this->data[$this->alias]['slug'] = $this->generateSlug($this->data[$this->alias]['title'], $this->data[$this->alias][$this->primaryKey]);
            } else {
                $this->data[$this->alias]['slug'] = $this->generateSlug($this->data[$this->alias]['title']);
            }
        }
		
		$created_by	=	CakeSession::read('Auth.User.id');

        if (isset($this->data[$this->alias][$this->primaryKey]) && !empty($this->data[$this->alias][$this->primaryKey]))
                $this->data[$this->alias]['modified_by'] = $created_by;
				else
		        $this->data[$this->alias]['created_by'] = $created_by;


//		$this->data[$this->alias]['created_by']	=	
        return true;
    }



}

